package com.nit.lab.controller.basic;

import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.basic.UserDTO;
import com.nit.lab.dto.system.TokenDTO;
import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.service.basic.UserService;
import com.nit.lab.utils.HttpRequestReader;
import com.nit.lab.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/user")
public class UserController {

    @Resource
    private UserService userService;
    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     * @Description: 获取数据列表
     * @Author: SN
     * @Date: 2018/10/30 18:15
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            tableRspDTO = userService.list4Table(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    /**
     * @Description: 新增记录
     * @Author: SN
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestHeader(name = "Access-Token") String token, @RequestBody UserDTO userDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            opResult.setMessage(userService.add(userDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 更新记录
     * @Author: SN
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody UserDTO userDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(userService.update(userDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 删除记录
     * @Author: SN
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String userIds = jsonRequest.getString("userIds");
            opResult.setMessage(userService.remove(userIds).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 重置密码
     * @Author: SN
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
    public OpResultDTO resetPwd(@RequestBody UserDTO userDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(userService.resetPwd(userDTO.getUserId(), userDTO.getLoginPwd()).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 登录账号重名验证
     * @Author: SN
     * @Date: 2020/02/31 11:02
     */
    @RequestMapping(value = "/validateLoginName", method = RequestMethod.POST)
    public OpResultDTO validateLoginName(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String loginName = jsonRequest.getString("loginName");
            if (userService.loginCheck(loginName) == null) {
                opResult.setMessage("success");
            } else {
                opResult.setMessage("error");
            }
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
    /**
     * @Description: 根据Id获取信息
     * @Author: lxr
     * @Date: 2020/03/25
     */
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public OpResultDTO getInfo(@RequestHeader(name = "Access-Token") String token) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            opResult.setMessage("success");
            opResult.setResult(userService.getByUserId(tokenDTO.getAccountId()));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
}
